<!--  -->
<template>
  <div class="meedu-main-body">
    <back-bar class="mb-30" title="课程附件管理"></back-bar>
    <div class="float-left mb-30">
      <el-button
        @click="
          $router.push({
            name: 'AttachCreate',
            query: { course_id: box.course_id },
          })
        "
        type="primary"
        >添加
      </el-button>
    </div>
    <div class="float-left">
      <div class="float-left">
        <el-table
          :header-cell-style="{ background: '#f1f2f9' }"
          :data="attach"
          class="float-left"
        >
          <el-table-column prop="id" label="ID" width="120"> </el-table-column>
          <el-table-column prop="name" label="附件名"> </el-table-column>
          <el-table-column prop="path" label="路径" width="500">
          </el-table-column>
          <el-table-column label="下载次数" width="150"
            ><template slot-scope="scope">
              <span>{{ scope.row.download_times }}次 </span>
            </template>
          </el-table-column>
          <el-table-column fixed="right" label="操作" width="60">
            <template slot-scope="scope">
              <el-link type="danger" @click="delItem(scope.row.id)"
                >删除
              </el-link>
            </template>
          </el-table-column>
        </el-table>
      </div>
    </div>
  </div>
</template>

<script>
//这里可以导入其他文件（比如：组件，工具js，第三方插件js，json文件，图片文件等等）
//例如：import 《组件名称》 from '《组件路径》';
import BackBar from "@/components/back-bar";

export default {
  //import引入的组件需要注入到对象中才能使用
  components: { BackBar },
  data() {
    //这里存放数据
    return {
      box: {
        course_id: this.$route.query.course_id,
      },
      loading: false,
      attach: [],
    };
  },
  //监听属性 类似于data概念
  computed: {},
  //监控data中的数据变化
  watch: {},
  //方法集合
  methods: {
    getAttach() {
      if (this.loading) return;
      this.loading = true;
      this.box.course_id = this.$route.query.course_id;
      this.$request.get(`course_attach`, { params: this.box }).then((res) => {
        this.loading = false;
        this.attach = res.data.data;
      });
    },
    delItem(id) {
      if (this.loading) return;
      this.$confirm("确认操作？", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.loading = true;
          this.$request
            .delete(`course_attach/${id}`)
            .then(() => {
              this.loading = false;
              this.$message.success(this.$t("common.success"));
              this.getAttach();
            })
            .catch((e) => {
              this.loading = false;
              this.$message.error(e.message);
            });
        })
        .catch(() => {});
    },
  },
  //生命周期 - 创建完成（可以访问当前this实例）
  created() {
    this.getAttach();
  },
  //生命周期 - 挂载完成（可以访问DOM元素）
  mounted() {},
  beforeCreate() {}, //生命周期 - 创建之前
  beforeMount() {}, //生命周期 - 挂载之前
  beforeUpdate() {}, //生命周期 - 更新之前
  updated() {}, //生命周期 - 更新之后
  beforeDestroy() {}, //生命周期 - 销毁之前
  destroyed() {}, //生命周期 - 销毁完成
  activated() {}, //如果页面有keep-alive缓存功能，这个函数会触发
};
</script>
<style lang='less' scoped>
</style>